package com.hll.javaweb.servlet.basic;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @author hll
 * @create 2022-05-10 12:14
 */
@WebServlet("/hello-parameter")
public class HelloParameter extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws UnsupportedEncodingException {
    req.setCharacterEncoding("UTF-8"); // post请求需要带上编码，否则中文乱码

    String username = req.getParameter("username");
    System.out.println("username = " + username);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    // http://localhost:8080/web/hello-parameter?
    // username=dadasd%E5%A4%A7
    // &soccerTeam=German&soccerTeam=Holland
    // 上述username写了中文，可以正常打印
    String username = req.getParameter("username");
    System.out.println("username = " + username);

    // checkbox多值打印
    String[] soccerTeams = req.getParameterValues("soccerTeam");
    for (int i = 0; i < soccerTeams.length; i++) {
      System.out.println("team " + i + "=" + soccerTeams[i]);
    }
  }
}
